package com.lee.transactionaldemo.service.nested;

import com.lee.transactionaldemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：liylw
 * @date ：Created in 2021/4/23 16:22
 */
@Service
public class Nested1 {

    @Autowired
    private UserService userService;

    @Autowired
    private Nested2 nested2;


    /**
     * a,b 都成功
     */
    public void test1(){
        userService.saveUserASuccess();
        nested2.testSuccess();
    }

    /**
     * a,b 都成功
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void test2(){
        userService.saveUserASuccess();
        nested2.testSuccess();
    }

    /**
     * a,b 插入，没有回滚
     */
    public void test3(){
        nested2.testSuccess();
        userService.saveUserAFail();

    }


    /**
     * a,b 回滚
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void test4(){
        nested2.testSuccess();
        userService.saveUserAFail();
    }



    /**
     * a,b 回滚
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void test5(){
        userService.saveUserASuccess();
        nested2.testFail();
    }



    /**
     * a插入，b回滚。 这个需要特别注意一下
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void test6(){
        userService.saveUserASuccess();
        // 执行到这有个savepoint
        try {
            nested2.testFail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
